package org.nf.trading.controller.app.circle;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.controller.BaseController;
import org.nf.trading.entity.Circle;
import org.nf.trading.service.app.circle.CircleService;
import org.nf.trading.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/circle")
public class CircleController extends BaseController {
    private final CircleService circleService;

    /**
     * 创建圈子
     * @param circle
     * @return
     */
    @PostMapping
    public ResultVO addCircle(@RequestBody Circle circle){
        Circle c = circleService.insert(circle);
        return success(c);
    }

    /**
     * 判断是否发布过商品
     * @param uid
     * @return
     */
    @GetMapping("/determineProduct/{uid}")
    public ResultVO determineProduct(@PathVariable("uid") Integer uid){
        boolean determined = circleService.determineProduct(uid, 0);
        return success(200,determined ? "发布过商品":"未发布商品",determined);
    }

    /**
     * 通过用户的id查询全部圈子
     * @param uid
     * @return
     */
    @GetMapping("/circlesUid/{uid}")
    public ResultVO listCirclesByUid(@PathVariable("uid") Integer uid){
        return success(circleService.listCirclesByUid(uid));
    }


    /**
     * 通过圈子id查询一个圈子(包括全部的帖子)
     * @param circleId
     * @return
     */
    @GetMapping("{circleId}")
    public ResultVO getCircleById(@PathVariable("circleId") Integer circleId){
        return success(circleService.getCircleById(circleId));
    }

    /**
     * 通过圈子类型id查询全部圈子
     * @param circleTypeId
     * @return
     */
    @GetMapping("/circlesTypeId/{circleTypeId}")
    public ResultVO getListCirclesByTypeId(@PathVariable("circleTypeId") Integer circleTypeId){
        List<Circle> circles = circleService.listCirclesByTypeId(circleTypeId);
        return success(circles);
    }

    /**
     * 通过圈子名称进行查找
     * @param circleName
     * @return
     */
    @GetMapping("/circlesName/{circleName}")
    public ResultVO getListCirclesByName(@PathVariable("circleName") String circleName){
        List<Circle> circles = circleService.listCirclesByName(circleName);
        return success(circles);
    }

    /**
     * 查询全部圈子
     * @return
     */
    @GetMapping
    public ResultVO getListCircle(){
        return success(circleService.selectAll());
    }
}
